package com.hongxun.design.ui.ui_listview.refreshlist;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hongxun.design.R;
import com.hongxun.design.activitys.BaseActivity;
import com.hongxun.design.fragments.BaseFragment;
import com.hongxun.design.ui.ui_listview.Swipemenulistview.SwipeMenuCreator;
import com.hongxun.design.ui.ui_listview.Swipemenulistview.SwipeMenuListView;

/**
 * Created by zerdoor_pc on 2016/6/2.
 */
public class SwipeRefreshListView extends SwipeRefreshLayout implements AbsListView.OnScrollListener {
    SwipeMenuListView listView;
    BaseAdapter adapter;
    Context context;
    BaseFragment baseFragment;
    BaseActivity baseActivity;
    View mListViewFooter;
    TextView swiperefresh_no;
    boolean canScroll = false;

    public SwipeRefreshListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    public boolean canChildScrollUp() {
        if (listView != null && listView instanceof AbsListView) {
            final AbsListView absListView = listView;
            return absListView.getChildCount() > 0
                    && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
                    .getTop() < absListView.getPaddingTop());
        }
        return super.canChildScrollUp();
    }

    @SuppressLint("ResourceAsColor")
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        listView = (SwipeMenuListView) findViewById(R.id.swiperefresh_listview);
        swiperefresh_no = (TextView) findViewById(R.id.swiperefresh_no);
        swiperefresh_no.setVisibility(GONE);
        listView.setOnScrollListener(this);
        mListViewFooter = LayoutInflater.from(context).inflate(R.layout.listview_footer, null,
                false);
        this.setColorScheme(
                android.R.color.holo_red_light,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_blue_bright
        );
        setDivider(0);
    }


    public void setMenuCreator(SwipeMenuCreator creator, SwipeMenuListView.OnMenuItemClickListener onMenuItemClickListener) {
        listView.setMenuCreator(creator);
        listView.setOnMenuItemClickListener(onMenuItemClickListener);
    }


    public void setPosition(int position) {
        listView.setSelection(position);
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener itemClickListener) {
        listView.setOnItemClickListener(itemClickListener);
    }

    public void setOnItemLongClicklistener(AdapterView.OnItemLongClickListener listener){
        listView.setOnItemLongClickListener(listener);
    }

    public void setAdapter(final BaseAdapter adapter) {
        this.adapter = adapter;
        if (listView != null) {
            listView.setAdapter(adapter);
            if (adapter.getCount() == 0) {
                swiperefresh_no.setVisibility(VISIBLE);
            } else {
                swiperefresh_no.setVisibility(GONE);
            }
        }
    }

    public void setAdapter(final BaseAdapter adapter, String s) {
        this.adapter = adapter;
        if (listView != null) {
            listView.setAdapter(adapter);
            if (adapter.getCount() == 0) {
                swiperefresh_no.setVisibility(VISIBLE);
                swiperefresh_no.setText(s);
            } else {
                swiperefresh_no.setVisibility(GONE);
            }
        }
    }


    /**
     * 监听listviewitem数量*
     */
    public void update() {
        if (adapter == null) return;
        adapter.notifyDataSetChanged();
        if (adapter.getCount() == 0) {
            //listView.setBackgroundResource(R.layout.listview_no);
            swiperefresh_no.setVisibility(VISIBLE);
        } else {
            //listView.setBackgroundColor(getResources().getColor(R.color.transparent));
            swiperefresh_no.setVisibility(GONE);
        }
    }

    public void update(String s) {
        adapter.notifyDataSetChanged();
        if (adapter.getCount() == 0) {
            //listView.setBackgroundResource(R.layout.listview_no);
            swiperefresh_no.setVisibility(VISIBLE);
            swiperefresh_no.setText(s);
        } else {
            //listView.setBackgroundColor(getResources().getColor(R.color.transparent));
            swiperefresh_no.setVisibility(GONE);
        }
    }

    public void setUpdate(String s) {

        swiperefresh_no.setVisibility(VISIBLE);
        swiperefresh_no.setText(s);

    }

    public void destory(SwipeRefreshListView view) {

        view = null;
    }


    public void setDivider(int h) {
        if (listView != null) {
            listView.setDividerHeight(h);
        }
    }

    /**
     * 设置能否上拉加载*
     */
    public void setCanScroll(boolean canScroll) {
        this.canScroll = canScroll;
    }

    /**
     * 设置上拉加载的界面*
     */
    public void setFragment(BaseFragment baseFragment) {
        this.baseFragment = baseFragment;
    }

    public void setFragment(BaseActivity baseActivity) {
        this.baseActivity = baseActivity;
    }


    @Override
    public void onScrollStateChanged(AbsListView absListView, int scrollState) {
        if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
//            setImage();
            // 判断是否滚动到底部
            if (absListView.getLastVisiblePosition() == absListView.getCount() - 1 && back() && canScroll) {
                listView.addFooterView(mListViewFooter);
                if (baseFragment != null) {
                    baseFragment.addMore();
                }
                if (baseActivity != null) {
                    baseActivity.addMore();
                }

            }
        }
    }

    private long mExitTime;

    private boolean back() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            mExitTime = System.currentTimeMillis();
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {

    }

    public void removeFooterView() {
        try {
            if (listView.getFooterViewsCount() > 0) {
                listView.removeFooterView(mListViewFooter);
            }
        } catch (Exception e) {
        }
    }

}
